package com.example.Service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.Dao.CollectMapper;
import com.example.Dao.FolderMapper;
import com.example.Entity.Dto.PageParmar;
import com.example.Entity.Dto.UserInfo;
import com.example.Entity.Po.tn_sys_collect;
import com.example.Entity.Po.tn_sys_folder;
import com.example.Entity.Vo.APIResult;
import com.example.IService.ICollectService;
import com.example.Utils.APIResultUtils;
import com.example.Utils.StringUtils;
import com.sun.org.apache.bcel.internal.generic.NEW;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.Map;

@Service
public class CollectService extends BaseService implements ICollectService {
    @Resource
    CollectMapper collectMapper;
    @Resource
    FolderMapper folderMapper;

    /**
     * 获取用户的收藏夹
     * @param pageParmar 分页
     * @return APIResult
     */
    @Override
    public APIResult GetUserCollect(PageParmar pageParmar) {
        UserInfo userInfo = GetSessionInfo();
        Map entity = (Map)pageParmar.getArgs();
        String foldertype = entity.getOrDefault("folderType","").toString();
        if (StringUtils.isBlank(foldertype))
            return APIResult.error("请传入文件夹类型");
        QueryWrapper<tn_sys_collect> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("userid",userInfo.getId());
        queryWrapper.eq("bstate",true);
        queryWrapper.eq("folderuuid",foldertype);
        if (StringUtils.isNotBlank(pageParmar.getKeyword()))
            queryWrapper.like("bname",pageParmar.getKeyword()).or().like("burl",pageParmar.getKeyword());
        queryWrapper.orderByDesc("createtime");
        IPage<tn_sys_collect> iPage = new Page<>(pageParmar.getPageNo(),pageParmar.getPageSize());
        IPage<tn_sys_collect> data = collectMapper.selectPage(iPage,queryWrapper);
        return APIResult.success("",data);
    }

    /**
     * 添加用户的收藏夹
     * @param sysCollect 数据
     * @return APIResult
     */
    @Override
    public APIResult AddUserCollect(tn_sys_collect sysCollect) {
        if(StringUtils.isBlank(sysCollect.getBname()))
            return APIResult.error("请填写名称");
        if(StringUtils.isBlank(sysCollect.getBexplain()))
            return APIResult.error("请填写链接介绍");
        if(StringUtils.isBlank(sysCollect.getBurl()))
            return APIResult.error("请填写链接地址");
        UserInfo userInfo = GetSessionInfo();
        QueryWrapper<tn_sys_folder> folderQueryWrapper = new QueryWrapper<>();
        folderQueryWrapper.eq("foldername",sysCollect.getFoldername());
        folderQueryWrapper.eq("userid",userInfo.getId());
        tn_sys_folder folder = folderMapper.selectOne(folderQueryWrapper);
        if (folder==null)
            return APIResult.error("不存在该文件夹");
        sysCollect.setBuuid(GetUUID());
        sysCollect.setBstate(true);
        sysCollect.setIsshare(false);
        sysCollect.setUsername(userInfo.getUsername());
        sysCollect.setUserid(userInfo.getId());
        sysCollect.setCreatetime(new Date());
        sysCollect.setFoldername(sysCollect.getFoldername());
        sysCollect.setFolderuuid(folder.getUuid());
        int c = collectMapper.insert(sysCollect);
        return c>0?APIResult.success():APIResult.error("添加失败");
    }

    /**
     * 删除用户的收藏夹
     * @param uuid 唯一标识符
     * @return APIResult
     */
    @Override
    public APIResult DeleteUserCollect(String uuid) {
        if (StringUtils.isBlank(uuid))
            return APIResult.error("请选择一个收藏夹内容进行删除");
        // 获取登录信息检测是否登录
        GetSessionInfo();
        QueryWrapper<tn_sys_collect> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("buuid",uuid);
        int c = collectMapper.delete(queryWrapper);
        return c>0?APIResult.success():APIResult.error("删除失败");
    }

    /**
     * 获取用户的垃圾桶内的收藏夹
     * @param pageParmar 分页
     * @return APIResult
     */
    @Override
    public APIResult GetUserRecycleBin(PageParmar pageParmar) {
        UserInfo userInfo = GetSessionInfo();
        QueryWrapper<tn_sys_collect> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("userid",userInfo.getId());
        queryWrapper.eq("bstate",false);
        if (StringUtils.isNotBlank(pageParmar.getKeyword()))
            queryWrapper.like("bname",pageParmar.getKeyword()).or().like("burl",pageParmar.getKeyword());
        queryWrapper.orderByDesc("createtime");
        IPage<tn_sys_collect> iPage = new Page<>(pageParmar.getPageNo(),pageParmar.getPageSize());
        IPage<tn_sys_collect> data = collectMapper.selectPage(iPage,queryWrapper);
        return APIResult.success("",data);
    }

    /**
     * 删除用户的收藏夹（假删除）
     * @param uuid 唯一标识符
     * @return APIResult
     */
    @Override
    public APIResult FalseDeleteUserCollect(String uuid) {
        if (StringUtils.isBlank(uuid))
            return APIResult.error("请选择一个收藏夹内容进行删除");
        // 获取登录信息检测是否登录
        GetSessionInfo();
        UpdateWrapper<tn_sys_collect> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("buuid",uuid);
        updateWrapper.set("bstate",false);
        int c = collectMapper.update(null,updateWrapper);
        return c>0?APIResult.success():APIResult.error("删除失败");
    }

    /**
     * 恢复用户的收藏夹
     * @param uuid 唯一标识符
     * @return APIResult
     */
    @Override
    public APIResult RecoverUserCollect(String uuid) {
        if (StringUtils.isBlank(uuid))
            return APIResult.error("请选择一个收藏夹内容进行恢复");
        // 获取登录信息检测是否登录
        GetSessionInfo();
        UpdateWrapper<tn_sys_collect> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("buuid",uuid);
        updateWrapper.set("bstate",true);
        int c = collectMapper.update(null,updateWrapper);
        return c>0?APIResult.success():APIResult.error("恢复失败");
    }

    /**
     * 获取所有的分享收藏夹
     * @param pageParmar 分页
     * @return APIResult
     */
    @Override
    public APIResult GetShareCollect(PageParmar pageParmar) {
        QueryWrapper<tn_sys_collect> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("bstate",true);
        queryWrapper.eq("isshare",true);
        if (StringUtils.isNotBlank(pageParmar.getKeyword()))
            queryWrapper.like("bname",pageParmar.getKeyword()).or().like("burl",pageParmar.getKeyword());
        queryWrapper.orderByDesc("createtime");
        IPage<tn_sys_collect> iPage = new Page<>(pageParmar.getPageNo(),pageParmar.getPageSize());
        IPage<tn_sys_collect> data = collectMapper.selectPage(iPage,queryWrapper);
        return APIResult.success("",data);
    }

    /**
     * 用户分享收藏夹
     * @param uuid 唯一标识符
     * @return APIResult
     */
    @Override
    public APIResult UserShareCollect(String uuid) {
        if (StringUtils.isBlank(uuid))
            return APIResult.error("请选择一个收藏夹内容进行分享");
        // 获取登录信息检测是否登录
        GetSessionInfo();
        UpdateWrapper<tn_sys_collect> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("buuid",uuid);
        updateWrapper.eq("bstate",true);
        updateWrapper.set("isshare",true);
        int c = collectMapper.update(null,updateWrapper);
        return c>0?APIResult.success():APIResult.error("分享失败");
    }

    /**
     * 取消分享
     * @param uuid 唯一标识符
     * @return APIResult
     */
    @Override
    public APIResult CancelCollect(String uuid) {
        if (StringUtils.isBlank(uuid))
            return APIResult.error("请选择一个收藏夹内容进行取消分享");
        // 获取登录信息检测是否登录
        GetSessionInfo();
        UpdateWrapper<tn_sys_collect> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("buuid",uuid);
        updateWrapper.eq("bstate",true);
        updateWrapper.set("isshare",false);
        int c = collectMapper.update(null,updateWrapper);
        return c>0?APIResult.success():APIResult.error("取消分享失败");
    }

    /**
     * 修改文件夹
     * @param uuid 收藏的guid
     * @param foldername 文件夹名称
     * @return APIResult
     */
    @Override
    public APIResult UpdateCollectFolder(String uuid, String foldername) {
        if (StringUtils.isEmpty(uuid) || StringUtils.isEmpty(foldername))
            return APIResult.error("请传入完整信息");
        QueryWrapper<tn_sys_folder> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("foldername",foldername);
        tn_sys_folder folder = folderMapper.selectOne(queryWrapper);
        if (folder==null)
            return APIResult.error("未有该文件夹");
        UpdateWrapper<tn_sys_collect> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("buuid",uuid);
        updateWrapper.set("foldername",folder.getFoldername());
        updateWrapper.set("folderuuid",folder.getUuid());
        int successcount = collectMapper.update(null,updateWrapper);
        return successcount>0?APIResult.success():APIResult.error("修改失败");
    }

    /**
     * 修改收藏内容
     * @param sysCollect 数据
     * @return APIResult
     */
    @Override
    public APIResult UpdateCollect(tn_sys_collect sysCollect) {
        if (sysCollect==null)
            return APIResult.error("未获取到具体的数据");
        QueryWrapper<tn_sys_folder> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("foldername",sysCollect.getFoldername());
        tn_sys_folder uu = folderMapper.selectOne(queryWrapper);
        if (uu==null)
            return APIResult.error("没有该文件夹");
        sysCollect.setFolderuuid(uu.getUuid());
        int successcount = collectMapper.updateById(sysCollect);
        return successcount>0?APIResult.success():APIResult.error("修改失败");
    }

    /**
     * 将公共的收藏添加到我的收藏中
     * @param uuid 唯一标识符
     * @param filename 文件夹名称
     * @return APIResult
     */
    @Override
    public APIResult AddCopyUserCollect(String uuid, String filename) {
        if (StringUtils.isEmpty(uuid))
            return APIResult.error("请传入收藏的记录");
        if (StringUtils.isEmpty(filename))
            return APIResult.error("请选择要收藏到的文件夹位置");
        QueryWrapper<tn_sys_collect> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("buuid",uuid);
        queryWrapper.eq("bstate",true);
        tn_sys_collect data = collectMapper.selectOne(queryWrapper);
        if (data==null)
            return APIResult.error("未找到要添加至该收藏记录");
        QueryWrapper<tn_sys_folder> folderQueryWrapper = new QueryWrapper<>();
        folderQueryWrapper.eq("foldername",filename);
        tn_sys_folder folder = folderMapper.selectOne(folderQueryWrapper);
        if (folder==null)
            return APIResult.error("未找到收藏夹");
        UserInfo userInfo = GetSessionInfo();
        data.setBuuid(GetUUID());
        data.setUserid(userInfo.getId());
        data.setUsername(userInfo.getUsername());
        data.setCreatetime(new Date());
        data.setFolderuuid(folder.getUuid());
        data.setFoldername(folder.getFoldername());
        data.setIsshare(false);
        int successcount = collectMapper.insert(data);
        return successcount>0? APIResult.success():APIResult.error("加入失败");
    }


}
